package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;


	public class DataBase {
	        
	         
	        private static DataBase instance = null;
	        
	        private static ConfigurationProperties _conf = new ConfigurationProperties();

//		private boolean connected;
	        private Connection conn = null;
	        private Statement stmt = null;
	        public final String separator = ";";
//		public String url =null;
	        
//		public Database () {}
	        
	        public static DataBase getInstance() {
	        if (instance == null) {
	            instance = new DataBase();
	        }
	        return instance;
	    }
	        
	        
	        public boolean isConnect(){
	                if(conn == null)
	                        return false;
	                try {
	                        
	                        return !conn.isClosed();
	                } catch (SQLException e) {
	                        e.printStackTrace();
	                }
	                return false;
	        }
	        
	        public void ConnectDB() throws Exception {
	                
	                if(!isConnect()){
	                        System.out.println();
	                        String url = "";
	                        try { 
                                Class.forName(_conf.getProperty("driver")).newInstance();
	                        		url = "jdbc:" + _conf.getProperty("dbtype") + "://" + 
	                                        _conf.getProperty("host") + ":" + _conf.getProperty("port") + "/"+ 
	                                        _conf.getProperty("dbname") + "?user=" + _conf.getProperty("user") + 
	                                        "&password=" + _conf.getProperty("pass"); 
		                               
	                                
	                                conn = DriverManager.getConnection(url);


	                        } catch (Exception e) {  
	                        visualizzaMess("Impossibile connettersi al DataBase");
	                        System.err.println("Got an exception during connection: "+url);  
	                        throw new Exception (e.getMessage());
	                                //e.printStackTrace();
	                        }  
	                }
	        }

	        public boolean InsertQuery(String Query){
	                try {
	                        ConnectDB();
	                } catch (Exception e1) {
	                        // TODO Auto-generated catch block
	                        e1.printStackTrace();
	                }
//			Log._printFile(Query+"\n", Log.logdir+"/sql.log");
	                if (isConnect()){
	                        try{
	                                stmt = conn.createStatement();
	                                stmt.execute(Query);
	                
	                                return true;
	                        }catch(Exception e){
	                visualizzaMess("Impossibile eseguire la query:\n"+Query);
	                                System.err.println("Got an exception in InsertQuery() ");  
	                                System.err.println(e.getMessage());
	                                return false;
	                        }
	                }
	                return true;
	        }
	        
	        public ResultSet SelectQuery(String Query){
	                try {
	                        ConnectDB();
	                } catch (Exception e1) {
	                        // TODO Auto-generated catch block
	                        e1.printStackTrace();
	                }
	        //	Log._printFile(Query+"\n", Log.logdir+"/sql.log");
	                ResultSet rs = null;
	                if (isConnect()){
	                        try{
	                                stmt = conn.createStatement();
	                                rs=stmt.executeQuery(Query);
	                        }catch(Exception e){
	                visualizzaMess("Impossibile eseguire la query:\n"+Query);
	                                System.err.println("Got an exception in SelectQuery() ");  
	                                System.err.println(e.getMessage());
	                                return null;
	                        }
	                }
	                
	                return rs;
	        }
	        
	        
	        
	        
	        
	        
	        
	        
	        
	        /**
	         * Funzione per la chiusura della connessione con il database
	         *
	         * @return true se la connessione e' stata chiusa false altrimenti
	         */
	        public boolean CloseDB(){
	                if(isConnect()){
	                        try { 
	                                conn.close();
//					connected=false;
	                                return true;
	                        } catch (Exception e) {  
	                                System.err.println("Got an exception during close connection ");  
	                                System.err.println(e.getMessage());
	                                return false;
	                        }  
	                }
	                return false;
	        }



	
	        
	        private void visualizzaMess(String msg){
	        }
	        
	        public HashSet<String> getFilmByGenre(String genre){
		    	   ResultSet res;
		    	   
		    	   HashSet<String> films= new HashSet<String>();
		    	 			
		    	   			
		    	   String  q="SELECT DISTINCT `titolo`" +
		           		   	 "FROM `film` " +
		           		     "WHERE genere='"+genre+"'";
		    	   					
		    	   System.out.println(q);
		    	   
			       res = SelectQuery(q);
						
			       try{
						if(res.first()){
							do {
								films.add(res.getString(1));			
							}while(res.next());
						}	
					}catch(SQLException e){
						System.err.println(e.getMessage());
					} 
			       
		    	   return films;
		       }
	        
	        
	        
	        
	        public HashSet<String> getFilmByGenre(String[] genre_list){
		    	   ResultSet res;
		    	   
		    	   HashSet<String> films= new HashSet<String>();
		    	   String genre="(\""+genre_list[0]+"\"";
		    	   
		    	 			    	   			
		    	   			for (int i=1; i<genre_list.length;i++){
		    	   				genre+=" || \""+genre_list[i]+"\"";
		    	   			}
		    	   				
		    	   			genre+=")";
		    	   			
		    	   			
		    	   String  q="SELECT DISTINCT `titolo`" +
		           		   	 "FROM `film` " +
		           		     "WHERE genere="+genre;
		    	   					
		    //	   System.out.println(q);
		    	   
			       res = SelectQuery(q);
						
			       try{
						if(res.first()){
							do {
								films.add(res.getString(1));			
							}while(res.next());
						}	
					}catch(SQLException e){
						System.err.println(e.getMessage());
					} 
			       
		    	   return films;
		       }        
	}



